﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports ISNet.WebUI.WebTreeView
Imports log4net

Partial Class FundedProgramRegen
    Inherits System.Web.UI.Page

    Dim cmmtcode As String = ""
    Public LifeConfig1 As String
    Public LifeConfig2 As String
    Public LifeConfig3 As String
    Public LifeAnalysisCost As String
    Public scripttext As String

    Private Shared logger As ILog = LogManager.GetLogger("FundedProgramRegen")

    Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        WebCombo3.EncodeOutput = True
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        cmmtcode = Session("CMMT_OT_CODE")
        PlanYearsTxt.Text = Session("PlanYear")
        VersionTxt.Text = Session("Versions")
        If Session("FCTR") Is Nothing Then
            Response.Redirect("Selector.aspx?type=1")
        End If
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        GenerateValues()

        If IsPostBack = False Then
            Session.Remove("Search")
            Session.Remove("search_fctr")
            search()
            WebGrid1.DataBind()
        End If

        Label1.Text = "ระบบจัดทำงบประมาณ >> จัดทำงบประมาณ >> สร้างรายการ FundedProgram ครุภัณฑ์ "
        'If Session("Versions") = 1 Then
        '    div1.Visible = False
        '    div2.Visible = False
        '    SearhBtn.Visible = False
        '    WebTreeView1.Visible = False
        '    WebTreeView2.Visible = False
        '    WebTreeView3.Visible = False
        'End If
        Dim adapt1 As New AssetGenTableAdapters.ActivityTypesTableAdapter
        Dim dat1 As New AssetGen.ActivityTypesDataTable
        dat1 = adapt1.GetData()
        ms8.DataSource = dat1
        ms8.DataTextField = "Description"
        ms8.DataValueField = "ActivityTypeCode"
        ms8.DataBind()

        'PlotNode()
        Dim appConfigAdapter As New AssuranceTableAdapters.AppConfigsTableAdapter
    End Sub


    Sub PlotNode()

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()

        Dim adapter As New FundCenterParentTableAdapters.fctr_viewTableAdapter
        Dim dt As New FundCenterParent.fctr_viewDataTable
        dt = adapter.GetDataByUser(Session("users"), Val(PlanYearsTxt.Text))

        Dim root_id As String

        MyNode.Text = "GSB"
        MyNode.Value = "0"
        root_id = "0"

        If dt.Count > 0 Then

            WebTreeView3.Nodes.Add(MyNode)

            Dim rootpath = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            rootpath = FctrClass.GetRootPrefil(Session("users"), Val(Session("planyear")), prefil)
            Dim rootarr() As String = Split(rootpath, ",")
            WebTreeView3.Nodes.Add(MyNode)

            For i = 0 To rootarr.Length - 1
                PlotChild(rootarr(i), MyNode, 1)
            Next
        End If

    End Sub

    Sub PlotChild(ByVal gpath As String, ByRef cnode As WebTreeViewNode, ByVal level As Decimal)
        'Response.Write(gpath & "<br>")
        Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
        Dim Hdatatable As New FundCenterParent.FctHDataTable
        Hdatatable = Hadapter.GetDataByGPath(gpath, Val(Session("planyear")))

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()
        For Each row As FundCenterParent.FctHRow In Hdatatable


            MyNode.Text = row.Description
            MyNode.Value = row.FundCenter_FundCenterCode

            cnode.Nodes.Add(MyNode)
        Next


        If Len(gpath) >= 10 Then
            Exit Sub
        End If

        Dim hadap2 As New FundCenterParentTableAdapters.UserLikeGpathTableAdapter
        Dim hdt2 As New FundCenterParent.UserLikeGpathDataTable
        Dim sql As String = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")

        Dim pathadd As String = "__"
        'For i = 1 To 4
        sql = ""
        Dim fullpath As String = gpath & pathadd
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " AND grouppath like '" & fullpath & "' "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND Fcth.PlanYear = " & Val(Session("PlanYear"))
        hadap2.GetDataByFilter(hdt2, sql)

        For Each row As FundCenterParent.UserLikeGpathRow In hdt2
            If Len(row.grouppath) <= 10 Then
                PlotChild(row.grouppath, MyNode, level + 1)
            End If

        Next

    End Sub


    Sub GenerateValues()
        If hdf1.Value.Length = 0 Then

        Else
            Try
                Dim Numx As Array = hdf1.Value.Split(",")

                For i = 0 To Numx.Length - 1

                    ms8.Items.FindByValue(Numx(i).ToString).Selected = True
                Next

            Catch ex As Exception
                logger.Error(ex.Message)
                logger.Error(ex.StackTrace)
            End Try
        End If

    End Sub


    'Protected Sub coppy_btn_Click(sender As Object, e As ImageClickEventArgs) Handles coppy_btn.Click

    '    Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

    '    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

    '    For Each keyValue As String In checkedRows

    '        adapter.CoppyRow(keyValue)

    '    Next keyValue

    '    WebGrid1.ClearCachedDataSource()
    '    WebGrid1.RebindDataSource()

    'End Sub

    'Protected Sub delete_btn_Click(sender As Object, e As ImageClickEventArgs) Handles delete_btn.Click

    '    Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

    '    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

    '    For Each keyValue As String In checkedRows

    '        adapter.DeleteRow(keyValue)

    '    Next keyValue



    '    WebGrid1.ClearCachedDataSource()
    '    WebGrid1.RebindDataSource()

    'End Sub



    Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo1.InitializeDataSource
        'If Session("Versions") > 1 Then
        'ds = PublicFunction.GetChild2(Session("users"))
        'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
        'e.DataSource = ds
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt



        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub WebCombo2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo2.InitializeDataSource
        'If Session("Versions") > 1 Then
        'ds = PublicFunction.GetChild2(Session("users"))
        'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
        'e.DataSource = ds
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt


        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub
    Protected Sub WebCombo3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        'If Session("Versions") > 1 Then
        'ds = PublicFunction.GetChild2(Session("users"))
        'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
        'e.DataSource = ds
        Dim dt As New DataTable
        dt.Columns.Add("AssetGroupName")
        dt.Columns.Add("AssetGroupType")
        dt.Rows.Add("เลือกทั้งหมด", 0)
        dt.Rows.Add("ครุภัณฑ์สำนักงาน", 1)
        dt.Rows.Add("ครุภัณฑ์คอมพิวเตอร์", 2)
        dt.Rows.Add("ครุภัณฑ์ขนส่ง", 3)
        e.DataSource = dt


        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        search()
        WebGrid1.RebindDataSource()       
    End Sub
    Private Sub search()
        Dim Adapter1 As New AssetGenTableAdapters.BudgetDetailsTableAdapter

        Dim adapter2 As New AssetGenTableAdapters.tempgenassetTableAdapter
        Dim dat1 As New AssetGen.BudgetDetailsDataTable
        Dim dat2 As New AssetGen.tempgenassetDataTable


        If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
            If Val(WebCombo1.Value) = 0 Or Val(WebCombo2.Value) = 0 Then
                Session("search_fctr") = ""
            Else
                Session.Remove("search_fctr")
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo2.Value, Session("users"), Val(PlanYearsTxt.Text)))
            End If

        ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
            If Val(WebCombo4.Value) = 0 Then
                Session("search_fctr") = ""
            Else
                Session.Remove("search_fctr")
                Session.Add("search_fctr", FctrClass.GetSubFundCenter(WebCombo4.Value, Session("users"), Session("Planyear"), Session("prefilter")))
            End If

        End If
        If TabContainer2.ActiveTab.ID = TabPanel3.ID Then
            If Val(WebCombo3.Value) = 0 Then
                Session("search_fctr2") = ""
            Else
                Session.Remove("search_fctr2")
                Session.Add("search_fctr2", GenAssetFromTypes(Val(WebCombo3.Value)))
            End If
        ElseIf TabContainer2.ActiveTab.ID = TabPanel4.ID Then
            If hdf1.Value.Length = 0 Then
                Session("search_fctr2") = ""
            Else
                Try
                    Dim Numx As Array = hdf1.Value.Split(",")
                    Dim result As String = "0"

                    For i = 0 To Numx.Length - 1
                        result += "," & Numx(i).ToString
                        ms8.Items.FindByValue(Numx(i).ToString).Selected = True
                    Next
                    Session.Remove("search_fctr2")
                    Session("search_fctr2") = result
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)
                End Try
            End If
            '   MsgBox(Session("search_fctr2"))
        End If
        Dim fctr = Session("search_fctr")
        Dim fctr2 = Session("search_fctr2")

        Dim checksql As String = ""
        checksql = " SELECT        FundCenter_FundCenterCode, ActivityType_ActivityTypeCode, Amount AS Sumamount, PricePerUnit AS sumPPU,  NewQuantity AS SumNewQuan, "
        checksql += " ReplaceQuantity AS SumReplace,PlanYear,BudgetDetails.id"
        checksql += " FROM BudgetDetails"
        checksql += " WHERE        (FormId = 'F04') AND Version = '" & VersionTxt.Text & "' AND PlanYear =" & PlanYearsTxt.Text & " AND ActivityType_ActivityTypeCode IS NOT NULL"
        If fctr <> "" Then
            checksql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & fctr & ") "
        ElseIf fctr2 <> "" Then
            checksql += " AND BudgetDetails.Activitytype_ActivityTypecode IN (" & fctr2 & ") "
        Else
            Dim prefil As String = ""
            prefil = Session("prefilter")
            If prefil <> "" Then
                checksql += " AND BudgetDetails.FundCenter_FundCenterCode IN (" & prefil & ") "
            End If
        End If


        Adapter1.GetDataByFilter(dat1, checksql)
        'Response.Write(checksql)
        adapter2.ClearAll()
        For Each row As AssetGen.BudgetDetailsRow In dat1
            dat2 = adapter2.GetDataByFCAT(row.FundCenter_FundCenterCode, row.ActivityType_ActivityTypeCode, row.PlanYear, row.id)
            If dat2.Count = 0 Then
                adapter2.InsertQuery(row.FundCenter_FundCenterCode, row.ActivityType_ActivityTypeCode, 1, row.SumNewQuan, row.SumReplace, row.Sumamount, row.sumPPU, row.PlanYear, row.id, Val(row.SumNewQuan + row.SumReplace))


            End If
        Next

        Session.Remove("Search")
        Session.Add("Search", "true")
        WebGrid1.ClearCachedDataSource()
        ' WebGrid1.RebindDataSource()

    End Sub

    Protected Sub HowtoCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles HowToCombo.InitializeDataSource
        Dim dt As New DataTable
        dt.Columns.Add("howtodesc")
        dt.Columns.Add("howtogen")
        dt.Rows.Add("สร้างรหัสปกติ", 1)
        dt.Rows.Add("สร้างรหัสรวม", 2)
        dt.Rows.Add("ไม่สร้างรหัส", 3)
        e.DataSource = dt

    End Sub
    Function GenAssetFromTypes(ByVal types As Decimal) As String
        Dim adpat1 As New AssetGenTableAdapters.ActivityTypesTableAdapter
        Dim data1 As New AssetGen.ActivityTypesDataTable
        data1 = adpat1.GetDataByGroup(types)

        Dim result As String = "0"
        For Each row As AssetGen.ActivityTypesRow In data1
            result += "," & "'" & row.ActivityTypeCode & "'"
        Next
        Return result
    End Function
    Protected Sub WebGrid1_InitializeDataSource(sender As Object, e As DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter1 As New AssetGenTableAdapters.GenDataListTableAdapter
        Dim dataX As New AssetGen.GendataListDataTable
        Dim SQL As String = ""
        Dim fctr = Session("search_fctr")
        Dim fctr2 = Session("search_fctr2")
        SQL = "SELECT tempgenasset.FundCenterCode, tempgenasset.ActivityTypecode, tempgenasset.howtogen, tempgenasset.add1, tempgenasset.return1, tempgenasset.amount, "
        SQL += " tempgenasset.PriceperUnit1, tempgenasset.PlanYear, FundCenters.Description AS FundCName, ActivityTypes.Description AS Acttype, ActivityTypes.AssetGroup, "
        SQL += " tempgenasset.genid, tempgenasset.BudgetDetail_Id, tempgenasset.sumcounter"
        SQL += " FROM tempgenasset INNER JOIN"
        SQL += " FundCenters ON tempgenasset.FundCenterCode = FundCenters.FundCenterCode INNER JOIN"
        SQL += " ActivityTypes ON tempgenasset.ActivityTypecode = ActivityTypes.ActivityTypeCode"

        If Session("Search") = "true" Then
            SQL += " Where 1=1 AND tempgenasset.ActivityTypeCode IS NOT NULL "
            SQL += " AND tempgenAsset.PlanYear = " & PlanYearsTxt.Text & " "
            SQL += " AND ActivityTypes.PlanYear = " & PlanYearsTxt.Text & " "
            SQL += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
            If fctr <> "" Then
                SQL += " AND tempgenasset.FundCenterCode IN (" & fctr & ") "
            ElseIf fctr2 <> "" Then
                SQL += " AND tempgenasset.ActivityTypecode IN (" & fctr2 & ") "
            Else
                Dim prefil As String = ""
                prefil = Session("prefilter")
                If prefil <> "" Then
                    SQL += " AND tempgenasset.FundCenterCode IN (" & prefil & ") "
                End If
            End If
            adapter1.GetDataByFilter(dataX, SQL)

            e.DataSource = dataX
            'ElseIf IsPostBack = False And Session("Search") = "true" Then
            '    SQL += " Where 1=1 AND tempgenasset.ActivityTypeCode IS NOT NULL "
            '    SQL += " AND tempgenAsset.PlanYear = " & PlanYearsTxt.Text & " "
            '    SQL += " AND ActivityTypes.PlanYear = " & PlanYearsTxt.Text & " "
            '    SQL += " AND FundCenters.PlanYear = " & PlanYearsTxt.Text & " "
            '    If fctr <> "" Then
            '        SQL += " AND tempgenasset.FundCenterCode IN (" & fctr & ") "
            '    ElseIf fctr2 <> "" Then
            '        SQL += " AND tempgenasset.ActivityTypecode IN (" & fctr2 & ") "
            '    Else
            '        Dim prefil As String = ""
            '        prefil = Session("prefilter")
            '        If prefil <> "" Then
            '            SQL += " AND tempgenasset.FundCenterCode IN (" & prefil & ") "
            '        End If
            '    End If
            '    adapter1.GetDataByFilter(dataX, SQL)
            '    e.DataSource = dataX
        Else
            e.DataSource = Nothing
        End If

        'Response.Write(SQL)


    End Sub

    Protected Sub WebGrid1_InitializePostBack(sender As Object, e As PostbackEventArgs) Handles WebGrid1.InitializePostBack
        If e.Action = PostBackAction.Custom Then
            Dim ActionType As String = TryCast(Request("type"), String)
            If ActionType = "1" Then
                Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

                Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

                For Each keyValue As String In checkedRows

                    adapter.CoppyRow(keyValue)

                Next keyValue
            Else
                Dim adapter1 As New AssetGenTableAdapters.tempgenassetTableAdapter


                Dim adapter2 As New AssetGenTableAdapters.FundedProgramAssetsTableAdapter

                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                    adapter2.DeleteRow(adapter1.GetBudDetailID(keyValue))
                    adapter1.DeleteRow(keyValue)

                Next keyValue
            End If

            WebGrid1.ClientAction.Refresh()
        End If
    End Sub

    Protected Sub WebGrid1_InitializeRow(sender As Object, e As RowEventArgs) Handles WebGrid1.InitializeRow
        e.Row.Cells(0).ForceNoEdit = True
        e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(2).ForceNoEdit = True
        e.Row.Cells(3).ForceNoEdit = True
        e.Row.Cells(4).ForceNoEdit = True
        e.Row.Cells(5).ForceNoEdit = True
        e.Row.Cells(6).ForceNoEdit = True
    End Sub

    Protected Sub WebGrid1_UpdateRow(sender As Object, e As RowEventArgs) Handles WebGrid1.UpdateRow
        Dim adapter1 As New AssetGenTableAdapters.tempgenassetTableAdapter
        Dim id As Decimal
        id = e.Row.KeyValue
        adapter1.UpdateHowTo(Val(e.Row.Cells(7).Value), id)
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        If WebGrid1.RootTable.Rows.Count = 0 Then
            Exit Sub
        End If
        GenerateValues()
        Dim adapter1 As New AssetGenTableAdapters.tempgenassetTableAdapter
        Session("currentcounter") = 1
        Dim data1 As New AssetGen.tempgenassetDataTable
        Dim adapter2 As New AssetGenTableAdapters.FundedProgramAssetsTableAdapter
        Dim data As New AssetGen.FundedProgramAssetsDataTable
        adapter2.ClearVersion(VersionTxt.Text)
        data1 = adapter1.GetDataByPlanYear(Val(PlanYearsTxt.Text))

        For Each row As AssetGen.tempgenassetRow In data1

            Select Case row.howtogen
                Case 1 : DoType1(row.ActivityTypecode, row.sumcounter, row.BudgetDetail_Id)

                Case 2 : DoType2(row.ActivityTypecode, row.BudgetDetail_Id)
                Case 3 : DoNothing()
            End Select
            'Response.Write(row.FundCenterCode & " " & row.ActivityTypecode & " " & row.BudgetDetail_Id & " " & row.sumcounter)
        Next

        logger.Info("User " & Session("users") & " re-generate funded program")

        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        scripttext = "<script>alert('สร้าง FundedProgram ครุภัณฑ์ เรียบร้อย');</script>"
    End Sub
    Sub DoType1(ByVal ActTypecode As String, ByVal add1 As Decimal, ByVal budid As Decimal)
        Dim adapter1 As New AssetGenTableAdapters.FundedProgramAssetsTableAdapter
        Dim data As New AssetGen.FundedProgramAssetsDataTable
        Dim startgen As String
        Dim valCounter As Decimal
        Dim FullFormat As String

        If ActTypecode(0).ToString = "9" Then
            startgen = Val(PlanYearsTxt.Text) & "9"
            valCounter = Val(adapter1.GetMax9()) + 1

            For i = valCounter To (valCounter + add1) - 1
                Dim ValCounter2 As String = ""
                ValCounter2 = Format(Session("currentcounter"), "0000000")
                FullFormat = startgen.Substring(2) & ValCounter2
                Session("currentcounter") = Session("currentcounter") + 1
                '  adapter1.InsertQuery()
                adapter1.InsertQuery(Val(PlanYearsTxt.Text), Val(PlanYearsTxt.Text), VersionTxt.Text, FullFormat, Date.Now(), Session("users"), budid)

            Next
        Else
            startgen = Val(PlanYearsTxt.Text) & "0"
            valCounter = Val(adapter1.GetMax0()) + 1
            'Response.Write(valCounter & " " & add1 & "</br>")
            For i = valCounter To (valCounter + add1) - 1
                Dim ValCounter2 As String = ""
                ValCounter2 = Format(Session("currentcounter"), "0000000")
                FullFormat = startgen.Substring(2) & ValCounter2
                Session("currentcounter") = Session("currentcounter") + 1
                'adapter1.InsertQuery()
                '  adapter1.InsertQuery()
                adapter1.InsertQuery(Val(PlanYearsTxt.Text), Val(PlanYearsTxt.Text), VersionTxt.Text, FullFormat, Date.Now(), Session("users"), budid)
            Next

        End If

    End Sub
    Sub DoType2(ByVal ActTypecode As String, ByVal budid As Decimal)
        Dim adapter1 As New AssetGenTableAdapters.FundedProgramAssetsTableAdapter
        Dim data As New AssetGen.FundedProgramAssetsDataTable
        Dim startgen As String
        Dim valCounter As Decimal
        Dim FullFormat As String
        Dim ValCounter2 As String = ""
        If ActTypecode(0).ToString = "9" Then
            startgen = Val(PlanYearsTxt.Text) & "9"
            valCounter = Val(adapter1.GetMax9()) + 1



            ValCounter2 = Format(valCounter, "0000000")
            FullFormat = startgen.Substring(2) & ValCounter2
            Session("currentcounter") = Session("currentcounter") + 1
            '  adapter1.InsertQuery()

            adapter1.InsertQuery(Val(PlanYearsTxt.Text), Val(PlanYearsTxt.Text), VersionTxt.Text, FullFormat, Date.Now(), Session("users"), budid)

        Else
            startgen = Val(PlanYearsTxt.Text) & "0"
            valCounter = Val(adapter1.GetMax0()) + 1


            ValCounter2 = Format(valCounter, "0000000")
            FullFormat = startgen.Substring(2) & ValCounter2
            Session("currentcounter") = Session("currentcounter") + 1
            'adapter1.InsertQuery()
            '  adapter1.InsertQuery()
            adapter1.InsertQuery(Val(PlanYearsTxt.Text), Val(PlanYearsTxt.Text), VersionTxt.Text, FullFormat, Date.Now(), Session("users"), budid)


        End If

    End Sub
    Sub DoNothing()
        Dim adapter1 As New AssetGenTableAdapters.FundedProgramAssetsTableAdapter
        Dim data As New AssetGen.FundedProgramAssetsDataTable

    End Sub

    Protected Sub delete_btn_Click(sender As Object, e As ImageClickEventArgs) Handles delete_btn.Click
        Dim adapter2 As New AssetGenTableAdapters.FundedProgramAssetsTableAdapter
        Dim data As New AssetGen.FundedProgramAssetsDataTable
        adapter2.ClearVersion(VersionTxt.Text)
        logger.Info("User " & Session("users") & " delete funded program")

        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        scripttext = "<script>alert('ลบข้อมูล FundedProgram ครุภัณฑ์ เรียบร้อย');</script>"
    End Sub


    Protected Sub WebCombo4_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo4.InitializeDataSource

        Dim adap As New FundCenterParentTableAdapters.SelectorTableAdapter
        Dim dt As New FundCenterParent.SelectorDataTable
        Dim dt2 As New FundCenterParent.SelectorDataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FctH.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += "ORDER BY Grouppath"
        adap.GetDataByFilter(dt2, sql)
        dt.Rows.Add("ทุกรายการ", "0", 0, "0", Val(Session("PlanYear")), "ทุกรายการ")
        dt.Merge(dt2)
        'dt.DefaultView.Sort = "FundCenter_FundCenterCode"
        e.DataSource = dt



    End Sub
End Class
